﻿using System;
using System.IO;
using System.Web.Mvc;
using Yz.Base;
using Yz.Core.Tools;

namespace Yz.Mvc.Controllers
{
    public class FileController : BaseController
    {
        /// <summary>
        /// 合并大文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult MergeUploadFile(string fileName, string guid)
        {
            var upFile = MergeFile(fileName, guid);
            return Json(upFile);
        }

        /// <summary>
        /// 文件上传(大文件分片)
        /// </summary>
        /// <param name="chunk"></param>
        /// <param name="chunks"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UploadBigFile(int? chunk, int? chunks, string guid)
        {
            var upFile = new VmUpFile();
            var file = Request.Files[0];
            if (chunks == null || chunks == 1)
            {
                upFile = SaveFile(FileType.Vdo, file);
                upFile.chunk = false;
                return Json(upFile);
            }
            var chu = Convert.ToInt32(chunk);
            upFile = SaveBigFile(chu, guid, file);
            upFile.chunk = true;
            return Json(upFile);
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="ft"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UploadFile(FileType ft)
        {
            var file = Request.Files[0];
            var upFile = SaveFile(ft, file);
            return Json(upFile);
        }


        /// <summary>
        /// 下载
        /// </summary>

        [AllowAnonymous]
        public void DownLoad(string filePath)
        {
            try
            {
                string filename = Path.GetFileName(filePath);
                FileInfo f = new FileInfo(filePath);
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(filename));
                Response.AddHeader("Content-Length", f.Length.ToString());
                if (filename.Substring(filename.LastIndexOf(".") + 1).ToString() == "exe")
                    Response.ContentType = "application/octet-stream";
                else if (filename.Substring(filename.LastIndexOf(".") + 1).ToString() == "pdf")
                    Response.ContentType = "application/ms-excel";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.Charset = "utf-8";
                Response.Filter.Close();
                Response.WriteFile(f.FullName);
            }
            catch (Exception ex)
            {
                YzNLog.Error(ex, "文件下载");
            }
            finally
            {
                Response.End();
            }
        }

        /// <summary>
        /// 文档预览（pdf）
        /// </summary>
        /// <returns></returns>

        [AllowAnonymous]
        public ActionResult Pdf(string flPath)
        {
            ViewBag.flPath = flPath;
            return View();
        }
        /// <summary>
        /// 视频播放
        /// </summary>
        /// <param name="flImgPath"></param>
        /// <param name="flPath"></param>
        /// <returns></returns>

        [AllowAnonymous]
        public ActionResult Video(string flImgPath, string flPath)
        {
            ViewBag.flPath = flPath;
            ViewBag.flImgPath = flImgPath;
            return View();
        }

    }
}